package com.aaa.stock.service.impl;

import cn.hutool.core.util.IdUtil;
import com.aaa.entity.StockPurchase;
import com.aaa.entity.StockPurchaseItem;
import com.aaa.stock.dao.PruchaseDao;
import com.aaa.stock.service.PurchaseService;
import com.aaa.stock.vo.PurchaseVo;
import com.aaa.util.JwtUtil;
import com.aaa.util.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Service
public class PurchaseServiceImpl extends ServiceImpl<PruchaseDao,StockPurchase> implements PurchaseService {

    @Autowired
    private PruchaseDao pruchaseDao;

    @Override
    public Result<IPage<StockPurchase>> getByPage(Integer currentPage, Integer pageSize, PurchaseVo purchaseVo) {
        IPage<StockPurchase>page=new Page<>(currentPage,pageSize);
        QueryWrapper<StockPurchase>wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(purchaseVo.getProviderId())){
            wrapper.eq("provider_id",purchaseVo.getProviderId());
        }
        if (StringUtils.hasText(purchaseVo.getApplyUserName())){
            wrapper.like("apply_user_name",purchaseVo.getApplyUserName());
        }
        if(Objects.nonNull(purchaseVo.getStatus())){
            wrapper.eq("status",purchaseVo.getStatus());
        }
        IPage<StockPurchase>page1=pruchaseDao.selectPage(page,wrapper);

        return new Result<>(2000,"查询审核",page1);
    }

    @Override
    public Result<String> purchaseIdRequest() {
        //  生成唯一id
        long purchaseId = IdUtil.getSnowflakeNextId();
        String head ="CG";
        // 拼接 标头  但是 不确定String.valueOf 会不会有精度丢失的情况
        String concat = head.concat(String.valueOf(purchaseId));
        return new Result<>(2000,"生成成功",concat);
    }

    @Override
    public Result<IPage<StockPurchase>> getByPage1(Integer currentPage, Integer pageSize, PurchaseVo purchaseVo) {
        IPage<StockPurchase>page=new Page<>(currentPage,pageSize);
        QueryWrapper<StockPurchase>wrapper=new QueryWrapper<>();

        if (StringUtils.hasText(purchaseVo.getProviderId())){
            wrapper.eq("provider_id",purchaseVo.getProviderId());
        }
        if (StringUtils.hasText(purchaseVo.getApplyUserName())){
            wrapper.like("apply_user_name",purchaseVo.getApplyUserName());
        }
        wrapper.eq("status",2);
        IPage<StockPurchase>page1=pruchaseDao.selectPage(page,wrapper);

        return new Result<>(2000,"查询审核",page1);



    }



    @Override
    public Result<String> Approved(String status, String purchaseId) {
    QueryWrapper<StockPurchase>queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("purchase_id",purchaseId);
     StockPurchase purchase = pruchaseDao.selectOne(queryWrapper);
        System.out.println("purchase ======================= " + purchase);
     String status1 = purchase.getStatus();
        System.out.println("status1 =========================== " + status1);
    if(status.equals("3")){
            if(status1.equals("3")){
                return new Result(2000,"该单 已经通过审核",0);

            }else {
                int i = pruchaseDao.modification(status,purchaseId);
                if(i==1){
                    return new Result(2000,"该单 审核通过",i);
                }

                return new Result(2000,"该单 没有修改成功",i);
            }
        }


        return new Result(5000,"服务器异常",0);


    }

    @Override
    public Result<String> NotApproved(String status, String purchaseId, String examine) {
        QueryWrapper<StockPurchase> queryWrapper = new QueryWrapper<StockPurchase>();
        queryWrapper.eq("purchase_id",purchaseId);
        StockPurchase purchase = pruchaseDao.selectOne(queryWrapper);
        String status1 = purchase.getStatus();
        if(status.equals("4")){
            if(status1.equals("4")){
                return new Result(2000,"该单 已经审核不通过",0);
            }else {
                int i = pruchaseDao.modificationE(status,purchaseId,examine);
                if (i == 1) {
                    return new Result(2000, "（审核不通过）修改成功", i);
                }

                return new Result(2000, "（审核不通过） 没有修改成功", i);
            }
        }

        return new Result(5000,"当前状态异常");
    }
    }



